﻿
using AI_Tank.Game;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AI_Tank.Game
{
    
    public class ParseRoundData
    {
        private int id;
        private List<Bullet> bullets = new List<Bullet>();
        private List<Player> players = new List<Player>();
        private List<Team> teams = new List<Team>();
        private List<BrickWall> brickwalls = new List<BrickWall>();
        private List<IronWall> ironwalls = new List<IronWall>();
        private List<River> rivers = new List<River>();
        private List<Star> stars = new List<Star>();
        private List<Coin> coins = new List<Coin>();
        private RoundData roundDataObj;
        public ParseRoundData(string roundData)
        {
            return;
            JObject obj = JObject.Parse(roundData);
            id = Int32.Parse(obj["msg_data"]["round_id"].ToString());
            JArray bulletsArr= JArray.Parse(obj["msg_data"]["bullets"].ToString());
            for (int i=0;i<bulletsArr.Count;i++)
            {
                BulletType bt;
                if (Int32.Parse(bulletsArr[i]["type"].ToString()) == 0)
                    bt = BulletType.Normal;
                else
                    bt = BulletType.Super;
                Position pos=new Position();pos.setPosition(Int32.Parse(bulletsArr[i]["x"].ToString()), Int32.Parse(bulletsArr[i]["y"].ToString()));
                Direction dir=new Direction();
                switch (bulletsArr[i]["direction"].ToString())
                {
                    case "up":dir = Direction.Up;break;
                    case "down": dir = Direction.Down; break;
                    case "left": dir = Direction.Left; break;
                    case "right": dir = Direction.Right; break;
                }
                Bullet bullet = new Bullet(bt, Int32.Parse(bulletsArr[i]["team"].ToString()), pos, dir);
                bullets.Add(bullet);
            }

            JArray brickwallsArr = JArray.Parse(obj["msg_data"]["brick_walls"].ToString());
            for (int i = 0; i < brickwallsArr.Count; i++)
            {
                Position pos = new Position();
                pos.setPosition(Int32.Parse(brickwallsArr[i]["x"].ToString()), Int32.Parse(brickwallsArr[i]["y"].ToString()));
                BrickWall bw = new BrickWall(pos);
                brickwalls.Add(bw);
            }

            JArray ironwallArr = JArray.Parse(obj["msg_data"]["iron_walls"].ToString());
            for(int i=0;i<ironwallArr.Count;i++)
            {

                Position pos = new Position();
                pos.setPosition(Int32.Parse(ironwallArr[i]["x"].ToString()), Int32.Parse(ironwallArr[i]["y"].ToString()));
                IronWall iw = new IronWall(pos);
                ironwalls.Add(iw);
            }

            JArray riverArr = JArray.Parse(obj["msg_data"]["river"].ToString());
            for (int i = 0; i < riverArr.Count; i++)
            {

                Position pos = new Position();
                pos.setPosition(Int32.Parse(riverArr[i]["x"].ToString()), Int32.Parse(riverArr[i]["y"].ToString()));
                River rv = new River(pos);
                rivers.Add(rv);
            }


            JArray starArr = JArray.Parse(obj["msg_data"]["stars"].ToString());
            for (int i = 0; i < starArr.Count; i++)
            {

                Position pos = new Position();
                pos.setPosition(Int32.Parse(starArr[i]["x"].ToString()), Int32.Parse(starArr[i]["y"].ToString()));
                Star st = new Star(pos);
                stars.Add(st);
            }

            JArray coinArr = JArray.Parse(obj["msg_data"]["coins"].ToString());
            for (int i = 0; i < coinArr.Count; i++)
            {

                Position pos = new Position();
                pos.setPosition(Int32.Parse(coinArr[i]["x"].ToString()), Int32.Parse(coinArr[i]["y"].ToString()));
                Coin co = new Coin(pos,Int32.Parse(coinArr[i]["point"].ToString()));
                coins.Add(co);
            }

               //  public Player(int id, int teamID, Position position, bool hasSuper)

            JArray playerArr = JArray.Parse(obj["msg_data"]["players"].ToString());
            for (int i = 0; i< playerArr.Count; i++)
            {

                Position pos = new Position();
        pos.setPosition(Int32.Parse(playerArr[i]["x"].ToString()), Int32.Parse(playerArr[i]["y"].ToString()));

                bool ifHasSuper;
                if (Int32.Parse(playerArr[i]["super_bullet"].ToString()) == 0)
                    ifHasSuper = false;
                else
                    ifHasSuper = true;
                Player pl = new Player(Int32.Parse(playerArr[i]["id"].ToString()), Int32.Parse(playerArr[i]["team"].ToString()),pos,ifHasSuper);
                players.Add(pl);
            }

            //public Team(int id,int point,int remainLife)

            JArray teamArr = JArray.Parse(obj["msg_data"]["teams"].ToString());
            for (int i = 0; i < teamArr.Count; i++)
            {
                int id = Int32.Parse(teamArr[i]["id"].ToString());
                int point= Int32.Parse(teamArr[i]["point"].ToString());
                int remainLife = Int32.Parse(teamArr[i]["remain_life"].ToString());
                Team t = new Team(id, point, remainLife);
                teams.Add(t);
            }




            // public RoundData(int id,List<Bullet> bullets,List<Player> players, 
            //List<Team> teams, List<BrickWall> brickwalls, List<IronWall> ironwalls,
            //List<River> rivers, List<Star> stars, List<Coin> coins)

            roundDataObj = new RoundData(id, bullets, players, teams, brickwalls, ironwalls, rivers, stars, coins);
        }
        public RoundData GetRoundDataObj()
        {
            return roundDataObj;
        }
    }
}
